package com.tpshion.deploy.task;

import com.alibaba.fastjson.JSON;
import com.tpshion.deploy.common.constant.MsgType;
import com.tpshion.deploy.common.constant.TaskStatus;
import com.tpshion.deploy.common.constant.TaskType;
import com.tpshion.deploy.common.support.MsgContext;
import com.tpshion.deploy.common.support.MsgData;
import com.tpshion.deploy.handler.SendMsgHandler;
import com.tpshion.deploy.task.support.TaskData;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public abstract class AbstartTaskHandler {

    @Resource
    protected SendMsgHandler sendMsgHandler;

    public abstract TaskType getTaskType();

    public String getType(){
        return getTaskType().getType();
    }

    public String taskName(){
        return getTaskType().getName();
    }

    public abstract void handleTask(TaskData taskData);

    public void startHandleTask(TaskData taskData){
        handleTask(taskData);
        afterHandleTask(taskData);
    }

    public void afterHandleTask(TaskData taskData){
        taskData.setStatus(TaskStatus.OK.getStatus());
        MsgData msgData = new MsgData();
        msgData.setType(MsgType.TASK.getType());
        msgData.setData(JSON.toJSONString(taskData));

        MsgContext msgContext = new MsgContext();
        msgContext.setTaskId(taskData.getTaskId());
        msgContext.setMsgData(msgData);
        sendMsgHandler.sendMsg(msgContext);
    }
}
